<?php
namespace App\Services;

//语智云喇叭
class YzService
{
    private static $instance;

    public $_data;

    const AccessKey_ID  = 'LTAI5tQ1881Gp887bGdLC7hf';
    const AccessKey_Secret = 'ORcXAEnkhVuy4N0mncEY9OxanzAmEo';

    const PROD_URI = 'https://mqtt.yzpos.com:5006/api/bank/pushmsg';

    //播报类型:
    //01-云闪付收款
    //02-支付宝收款
    //03-微信收款
    //10-取消支付
    public static function getInstance ()
    {
        if (! isset(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function params ($data)
    {
        $data['accessKeyId'] = self::AccessKey_ID;
        $data['requestId'] = uniqid();


        $microtime = microtime(true);
        $millisecondsTimestamp = (int)($microtime * 1000);

        $data['timestamp'] = (string)$millisecondsTimestamp;
        $str = self::getSignContent($data);

        $data['sign'] = self::sign($str);
        $this->_data = json_encode($data);
        return $this;
    }

    //参数拼接
    public  function getSignContent($params)
    {
        ksort($params);
        $stringToBeSigned = "";
        $i = 0;
        foreach ($params as $k => $v) {
            if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
                // 转换成目标字符集
                $v = $this->characet($v, 'UTF-8');

                if ($i == 0) {
                    $stringToBeSigned .= "$k" . "=" . "$v";
                } else {
                    $stringToBeSigned .= "&" . "$k" . "=" . "$v";
                }
                $i++;
            }
        }
        unset ($k, $v);
        return $stringToBeSigned;
    }

    public  function sign ($str)
    {
        $hex = hash_hmac('sha256', $str, self::AccessKey_Secret);
        $binary = hex2bin($hex);
        return base64_encode($binary);
    }

    public function characet($data, $targetCharset)
    {
        if (!empty($data)) {
            if (strcasecmp('UTF-8', $targetCharset) != 0) {
                $data = mb_convert_encoding($data, $targetCharset);
            }
        }
        return $data;
    }

    protected function checkEmpty($value)
    {
        if (!isset($value)) return true;
        if ($value === null) return true;
        if (trim($value) === "") return true;

        return false;
    }


    //随机字符串
    public static function nonce_str()
    {
        return date('YmdHis', time()) . rand(10000, 99999);
    }

    public function build ($url)
    {
        error_log(date('Y-m-d H:i:s')." 【传盛语智云喇叭请求】： ".json_encode([$url, $this->_data])."\n\n", 3, "/tmp/". 'csyz'.date('Ymd').".log");
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($this->_data)));
        $result = curl_exec($ch);
        $error = curl_error($ch);
        if ($error) {
            //error_log(date('Y-m-d H:i:s')." 【联迪云喇叭报错】： ".json_encode($error)."\n\n", 3, "/tmp/". 'landi'.date('Ymd').".log");
        }
        curl_close($ch);
        error_log(date('Y-m-d H:i:s')." 【传盛语智云喇叭响应】： ".json_encode($result)."\n\n", 3, "/tmp/". 'csyz'.date('Ymd').".log");
        return $result;
    }
}

